<?php

namespace app\admin\model;
use think\Model;
use think\Db;
use think\Session;

class Admin extends Model{
    protected $token = '_IdoNotKnowWhatTheKeyIs__LetMeGo';
    public function login($params,$url){
//      比对用户名和密码是否正确
        $tokenKey=$this->token;
        $admin_account = $params['admin_account'];
        $admin_password = md5(md5($tokenKey . $params['admin_password'] . $tokenKey));
        $userinfo = $this->where(['admin_account'=>$admin_account,'admin_password'=>$admin_password])->find();
        if(!$userinfo)
        {
            unset($params['admin_password']);
            return ['code'=>0,'msg'=>'用户名或密码不正确','data'=>$params,'jump'=>''];
        }
        else{
            $last_login_time = date('Y-m-d H:i:s',time());
            $this->where(['admin_account'=>$admin_account,'admin_password'=>$admin_password])->update(['last_login_time'=>$last_login_time]);
            //将用户信息存入到session中
            $arr =[
                'id' => $userinfo['id'],
                'admin_account'=>$userinfo['admin_account'],
                'admin_nickname'=>$userinfo['admin_nickname'],
                'last_login_time'=>$userinfo['last_login_time'],
                'admin_email'=>$userinfo['admin_email'],
                'admin_phone'=>$userinfo['admin_phone'],
                'admin_privilege'=>$userinfo['admin_privilege'],
                'admin_sex'=>$userinfo['admin_sex'],
                'admin_role'=>$userinfo['admin_role'],
                'admin_department'=>$userinfo['admin_department'],
                'admin_favicon'=>$userinfo['admin_favicon'],
            ];
            session('admin',$arr);
            $url = explode('refer=',$url);
            if(isset($url[1])){
                $jump = $url[1];
                $data = ['code'=>1,'msg'=>'登录成功！','data'=>$arr,'jump'=>$jump,'jump_url'=>true];
            }
            else{
                //优先跳转他有权限的系统
                $privilege =$arr['admin_privilege'];
                if($privilege=='root'){
                    $res = Db::name('top_nav')
                        ->where(['is_hide'=>0,'is_delete'=>0])
                        ->select();
                    $url = '/admin/' . $res[0]['module'] . '/index';
                    $data = ['code'=>1,'msg'=>'登录成功!','data'=>$arr,'jump'=>$url];
                }
                else{
                    $privilege = decode( $arr['admin_privilege'] );
                    $arr1 = [];
                    foreach ($privilege as $k=>$v){
                        array_push($arr1,$k);
                    }
                    if(!empty($k)){
                        $data = ['code'=>1,'msg'=>'登录成功!','data'=>$arr,'jump'=>'/admin/' . $arr1[0] . '/index'];
                    }
                    else{
                        $data = ['code'=>0,'msg'=>'无任何权限!','data'=>$arr,'jump'=>'/admin/member/login'];
                    }
                }
            }
            return $data;
        }

    }

    //暂时没有邀请码之说
    public function reg($params){
        $tokenKey=$this->token;
        $account = $this->whereIn('admin_account',$params['admin_account'])->find();
        if($account){
            $data = ['code'=>0,'msg'=>'用户名已存在！','data'=>[],'jump'=>''];
        }
        else{
            $params['admin_password'] = md5(md5($tokenKey . $params['admin_password'] . $tokenKey));
            $res = $this->allowField(true)->save($params);
            if($res){
                $data = ['code'=>1,'msg'=>'注册成功！','data'=>[],'jump'=>'/admin/member/login'];
            }
            else{
                $data = ['code'=>0,'msg'=>'注册失败！','data'=>[],'jump'=>'/admin/member/login'];
            }
        }
        return $data;
    }

    public function change($params){
        $tokenKey=$this->token;
        $old = $params['old'];
        $new = $params['new'];
        $admin =  session('admin');
        $userinfo = $this->where('admin_account',$admin['admin_account'])->find();
        $md5old = md5(md5($tokenKey . $old . $tokenKey));
        $md5new = md5(md5($tokenKey . $new . $tokenKey));
        if($userinfo['admin_password']!==$md5old){
            $data = ['code'=>0,'msg'=>'旧密码不一致！','data'=>[]];
        }
        else{
            $res = $this->where('admin_account',$admin['admin_account'])->update(['admin_password'=>$md5new]);
            if($res){
                $data = ['code'=>1,'msg'=>'修改成功！','data'=>[]];
                Session::delete('admin');
            }
            else{
                $data = ['code'=>0,'msg'=>'修改失败！','data'=>[]];
            }
        }
        return $data;
    }

}